<template>
  <div class="ranks">
    <div class="back">
      <i class="iconfont iconxiangzuo4 fa-angle-left" @click="back"></i>
      <h1 class="title">排行榜</h1>
    </div>
    <div class="rank" ref="rank">
      <div class="toplist" ref="toplist">
        <ul>
          <li class="item" v-for="item in yunTopList" :key="item.id" @click="selectItem(item)">
            <div class="icon">
              <img width="100" height="100" v-lazy="item.coverImgUrl"/>
            </div>
            <ul class="songlist">
              <li class="song" v-for="(song, index) in item.top" :key="index">
                <span>{{ index + 1 }}</span>
                <span>{{ song.name }} - {{ song.ar[0].name }}</span>
              </li>
            </ul>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script type="text/javascript">
import { getTop } from '@/api/rank'
// import { playlistMixin } from '@/common/js/mixin'
import { mapMutations, mapGetters } from 'vuex'
import BScroll from 'better-scroll'

const YUNMUSIC_TOP = [0, 1, 2, 3, 4, 22, 23]

export default {
  data () {
    return {
      yunTopList: []
    }
  },
  methods: {
    back () {
      this.setFindNoneq(false)
      this.$router.back()
    },
    _getTop () {
      for (var i = 0; i < YUNMUSIC_TOP.length; i++) {
        getTop(YUNMUSIC_TOP[i]).then((res) => {
          // console.log(res)
          var list = res.data.playlist
          list.top = res.data.playlist.tracks.slice(0, 3)
          this.yunTopList.push(list)
        })
      }
      console.log(this.yunTopList)
    },
    handlePlaylist (srol) {
      const bottom = this.currentSong.id ? '60px' : ''
      // console.log(this.currentSong.id)
      this.$refs.rank.style.bottom = bottom
      srol.refresh()
    },
    selectItem (item) {
      this.$router.push({
        path: `/toplist/${item.id}`
      })
      this.setTopList(item)
    },
    ...mapMutations({
      setFindNoneq: 'SET_FINDNONEQ_STATE',
      setTopList: 'SET_TOP_LIST'
    })
  },
  created () {
    this._getTop()
  },
  mounted () {
    const options = {
      click: true,
      tap: true
    }
    setTimeout(() => {
      this.scroll = new BScroll(this.$refs.toplist, options)
      var srol = this.scroll
      this.handlePlaylist(srol)
    }, 200)
  },
  computed: {
    ...mapGetters([
      'currentSong'
    ])
  }
}

</script>

<style lang="less" scoped>
@import "~@/common/css/variable";
@import "~@/common/css/mixin";

.ranks {
  height: 100%
}
.back {
  height: 50px;
  line-height: 50px;
  .fa-angle-left {
    display: inline-block;
    font-size: 43px;
    margin-right: 20px;
  }
  .title {
    display: inline-block;
  }
}

ul {
  padding: 0;
  margin: 0;
}
.rank {
  position: fixed;
  width: 100%;
  top: 50px;
  bottom: 0;
  .toplist {
    height: 100%;
    overflow: hidden;
    padding-top: 5px;
    .item {
      display: flex;
      margin: 0 10px;
      padding: 3px 0;
      height: 100px;
      list-style: none;
      border-bottom: 1px solid rgb(228, 228, 228);
      &:last-child {
        padding-bottom: 20px;
      }
      .icon {
        flex: 0 0 100px;
        width: 100px;
        height: 100px;
        img {
          border-radius: 3px;
        }
      }
      .songlist {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        padding: 0 20px;
        height: 100px;
        overflow: hidden;
        list-style: none;
        // background: @color-dialog-background;
        color: @color-text;
        font-size: @font-size-small-x;
        .song {
          text-overflow: ellipsis;
          overflow: hidden;
          white-space: nowrap;
          line-height: 30px;
        }
      }
    }
    .loading-container {
      position: absolute;
      width: 100%;
      top: 50%;
      transform: translateY(-50%);
    }
  }
  .loading-content {
    position: fixed;
    width: 100%;
    top: 70%;
    transform: translateY(-50%);
  }
}
</style>
